
const Dict = require('./dict')
const utils = require('./utils')
const path = require('path')
const fs = require('fs')
const dict_config_keys = require('./dict_config_keys')
const dict_maker_proxy = require('./dict_maker/dict_maker_proxy')

const dict_file_handler = {
    /**
     * 
     * @param {*} dict_folder 
     * @param {*} dst_folder 
     * @returns dict file path
     */
    async make(dict_folder, dst_folder, dst_name) {
        return await dict_maker_proxy.proxy_make(dict_folder, dst_folder, dst_name)
    },

    /**
     * 
     * @param {*} dict_file 
     * @param {*} dst_folder 
     * @returns {*}
     * {
     *     version:
     *     name:
     *     path:
     * }
     */
    async parse(dict_file, dst_folder, force) {
        // todo:
        // force
        const tempDict = new Dict(dict_file)
        if (!tempDict.isValid()) return null;
        const version = tempDict.getConfig(dict_config_keys.version)
        const dstFullPath = path.join(dst_folder, tempDict.getConfig(dict_config_keys.id) + '.fishdict')
        utils.makeDir(dst_folder)
        const retObj = {
            version,
            name: tempDict.getConfig(dict_config_keys.name),
            path: dstFullPath
        }
        tempDict.destroy();

        try {
            if (dict_file !== dstFullPath) {
                fs.cpSync(
                    dict_file,
                    dstFullPath
                );
            }
        } catch (e) {
            console.log('..parse exception', e)
            return null
        }

        return retObj
    }
}


module.exports = dict_file_handler;